package us.mooresr.webtroopmanager.client.activity;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.place.Validate;
import us.mooresr.webtroopmanager.client.ui.ValidationView;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class ValidationActivity extends MyActivity implements ValidationView.Presenter {

	public ValidationActivity(Validate place, ClientFactory clientFactory) {
		super(place, clientFactory);
		
	}
	
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		
		ValidationView validationView = clientFactory.getValidationView();
		validationView.setPresenter(this);
		panel.setWidget(validationView.asWidget());	
		
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getValidationView().updateSuccess(false);
				
			}

			@Override
			public void onSuccess(Boolean result) {
				clientFactory.getValidationView().updateSuccess(result);
				
			}
			
		};
		
		troopManagerService.validateUser(((Validate)place).getValidationChallenge(), callback);
	}

}
